package com.gongda.gulimall.order.service.impl;

import com.gongda.gulimall.order.vo.OrderReturnApplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gongda.gulimall.common.utils.PageUtils;
import com.gongda.gulimall.common.utils.Query;

import com.gongda.gulimall.order.dao.OrderReturnApplyDao;
import com.gongda.gulimall.order.entity.OrderReturnApplyEntity;
import com.gongda.gulimall.order.service.OrderReturnApplyService;
import org.springframework.util.StringUtils;


@Service("orderReturnApplyService")
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyDao, OrderReturnApplyEntity> implements OrderReturnApplyService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<OrderReturnApplyEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("sku_id",key)
                    .or().like("member_username",key);
        }
        IPage<OrderReturnApplyEntity> page = this.page(
                new Query<OrderReturnApplyEntity>().getPage(params),
                queryWrapper
        );
//        transform(page);
        return new PageUtils(page);
    }

    public void transform(IPage<?> page){
        List<OrderReturnApplyEntity> list = (List<OrderReturnApplyEntity>) page.getRecords();
        List<OrderReturnApplyVO> voList = new ArrayList<>();
        list.stream().forEach(r->{
            OrderReturnApplyVO orderReturnApplyVO = new OrderReturnApplyVO();
            BeanUtils.copyProperties(r,orderReturnApplyVO);
            if (r.getStatus() != null){
                if (r.getStatus() == 0){
                    orderReturnApplyVO.setStatusStr("待处理");
                } else if (r.getStatus() == 1){
                    orderReturnApplyVO.setStatusStr("退货中");
                } else if (r.getStatus() == 2){
                    orderReturnApplyVO.setStatusStr("已完成");
                } else {
                    orderReturnApplyVO.setStatusStr("已拒绝");
                }
            }
            voList.add(orderReturnApplyVO);
        });
    }

}